package org.shangrila.bme.mb;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;
import org.shangrila.bme.eo.BmePayroll;
import org.shangrila.comm.dao.DbAccess;
import org.shangrila.comm.helper.DateHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@SessionScoped
public class PayrollQueryMB implements Serializable {
	private static final long serialVersionUID = -50034321213217112L;
	private final Logger log = LoggerFactory.getLogger(this.getClass());

    private BmePayroll io = new BmePayroll();
	private List<BmePayroll> voc = new ArrayList<BmePayroll>(); 
	private BmePayroll selectedd = new BmePayroll();//选择详情信息

	private Date beginTime;	//起始时间
	private Date endTime;		//结束时间

	@Inject
	private DbAccess db;

	@PostConstruct
    public void init() {

    }
         
    //查询信息
	public String queryAction() {	
		StringBuilder sql = new StringBuilder("SELECT m FROM BmePayroll m ");
		StringBuilder where = new StringBuilder(" WHERE ");
		String para = "";
		
		if(null!=io.getId() && !"".equals(io.getId())) {
			where.append("m.id=?1");
			para = io.getId();
		}
		else if(null!=io.getContName() && !"".equals(io.getContName())) {
			where.append("m.contName like ?1");
			para = io.getContName()+"%";
		}
		else if(null != beginTime && null != endTime ) {
			where.append("m.reqDate BETWEEN ?1 AND ?2 ");
			String sTime = DateHelper.dateToStr(beginTime, DateHelper.DF_YYYY_MM_DD)+" 00:00:00";
			String eTime = DateHelper.dateToStr(endTime, DateHelper.DF_YYYY_MM_DD)+" 23:59:59";
			beginTime = DateHelper.strToDate(sTime, DateHelper.DF_DEFAULT);
			endTime  = DateHelper.strToDate(eTime, DateHelper.DF_DEFAULT);
		}
		
		else {
			where = new StringBuilder("");
		}
		
		log.info("queryAction..." + sql);

		if(where.toString().equals(""))		
			voc = db.findBySql(sql.toString());
		else {
			sql.append(where);
			if(sql.toString().indexOf("BETWEEN")>0)
				voc = db.findBySql(sql.toString(),new Object[]{beginTime,endTime});
			else
				voc = db.findBySql(sql.toString(),new Object[]{para});
		}
		if(voc.size()>0) {
			selectedd = voc.get(0);
		}
		log.info("voc..." + voc.size());
		return null;
	}
	
    public void onRowSelected(SelectEvent event) {
    	selectedd = (BmePayroll)event.getObject(); 
    }

	public BmePayroll getIo() {
		return io;
	}

	public void setIo(BmePayroll io) {
		this.io = io;
	}

	public List<BmePayroll> getVoc() {
		return voc;
	}

	public void setVoc(List<BmePayroll> voc) {
		this.voc = voc;
	}

	public BmePayroll getSelectedd() {
		return selectedd;
	}

	public void setSelectedd(BmePayroll selectedd) {
		this.selectedd = selectedd;
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

}